package com.rrd.dw.mr.gzip.kafka;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.mapreduce.Mapper.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.euler.commons.web.JsonMapperBuilder;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.kafka.CrawlResult;
import com.ucredit.babel.common.kafka.PostRecord;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.behavior.UserBehaviorRecord;
import com.ucredit.babel.common.model.call.CallRecord;
import com.ucredit.babel.common.model.call.CallUserInfo;
import com.ucredit.babel.common.model.call.SmsBill;
import com.ucredit.babel.common.model.dhb.DhbItagInfo;
import com.ucredit.babel.common.model.dhb.DhbItagInfoRecords;
import com.ucredit.babel.common.model.alibaba2.AliPayOrder;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoInfo;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoOrder;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoOrderProduct;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoPayInfo;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoRecord;
import com.ucredit.babel.common.model.alibaba2.MessageType;
import com.ucredit.babel.common.model.taobao.TaobaoItem;
@Deprecated
public class DHBtest {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	public static void main(String[] args) throws Exception {
		readFile();
		
	}
  public static void readFile() throws Exception{
	  String line="";
	  File file = new File("d:/babel.txt");
	  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
	  line=br.readLine();
	  while(line!=null){
		  System.out.println(line);
		  parseJson(line);
		  line=br.readLine();
		 
	  }
	 br.close();
	  
	 
	   
  }
  public static void parseJson(String value) throws Exception{
	  PostRecord postRecord = null;
		DhbItagInfoRecords dhbTagInfoRecords = new DhbItagInfoRecords();
		DHBTagInfoParse dhbTagInfoParse  = new DHBTagInfoParse();
		postRecord = JsonMapperBuilder.build().readValue(value.toString(), PostRecord.class);
		JSONObject jsonObj = JSON.parseObject(value);
		String type  = jsonObj.getString("type");
		
		 
		if (jsonObj.getString("type").equals("DHB_ITAGINFO")) {
			postRecord = JsonMapperBuilder.build().readValue(value.toString(), PostRecord.class);
			if (postRecord.getAction().equals("PUT")) {
				String data_json="";
				dhbTagInfoRecords = (DhbItagInfoRecords) postRecord.getRecord();
				if (dhbTagInfoRecords != null) {
					try{
						DhbItagInfo tagInfo = dhbTagInfoRecords.getItagInfo();
				    	if(tagInfo==null){
				    		return;
				    	}
				    	 data_json = tagInfo.getData();
				    	if(data_json==null || data_json.trim().equals("") ){
				    		return;
				    	}
				    
				    JSONObject	dataObj=JSON.parseObject(data_json);
					}catch(Exception e){
						 System.out.println("==="+data_json);
						System.out.println(value);
						throw e;
					}

				}
			}
		} else if (jsonObj.getString("type").equals("USER_BEHAVIOR")) {
			postRecord = JsonMapperBuilder.build().readValue(value.toString(), PostRecord.class);
			if (postRecord.getAction().equals("PUT")) {
				UserBehaviorParse userBehaviorParse = new UserBehaviorParse(); 
				UserBehaviorRecord behaviorRecord = (UserBehaviorRecord) postRecord.getRecord();
				if (behaviorRecord != null) {
					   if(behaviorRecord.getJob().getJobID().equals("e16340fc-c5ea-49f2-8ee6-c880016499fd")){
//						   userBehaviorParse.parseUserBehaviorBase(null, behaviorRecord);
							userBehaviorParse.parseUserBehaviorEvent(null, behaviorRecord);
					   }
					   
				} else {
					 System.out.println("behaviorRecord==null");
					 System.out.println(value);
				}
			}
		} else if (jsonObj.getString("type").equals("CALL_HISTORY")) {
			postRecord = JsonMapperBuilder.build().readValue(value.toString(), PostRecord.class);

			if (postRecord.getAction().equals("CLONE")) {
				// String[] arr = (String[]) postRecord.getRecord();
				// context.write(new Text("clone"), new
				// Text(arr[0]+CRT001+arr[1]+CRT001+(type==null?"":type)));
				return;
			} else if (postRecord.getAction().equals("PUT")) {
				CallRecord callRecord = (CallRecord) postRecord.getRecord();
                Job job = callRecord.getJob();
                CrawlResult result=job.getResult();
                CallUserInfo user=callRecord.getUserInfo();
                List<SmsBill> list=callRecord.getSmsBills();

			}
		}else if(jsonObj.getString("type").equals("ALITAOBAO")) {
			postRecord = JsonMapperBuilder.build().readValue(value.toString(), PostRecord.class);
			if (postRecord.getAction().equals("PUT")) {
				AliTaobaoRecord aliTaobaoRecord = (AliTaobaoRecord) postRecord.getRecord();
				if (aliTaobaoRecord != null) {
					AliTaobaoInfo taobaoInfo= aliTaobaoRecord.getTaobao();
			    	AliTaobaoPayInfo payInfo  = aliTaobaoRecord.getAliTaobaoPayInfo();
			    	MessageType messageType = aliTaobaoRecord.getMessType();
			    	List<AliTaobaoOrder> list=taobaoInfo.getOrders();
					if(list==null || list.size()<=0){
						return;
					}
					for(AliTaobaoOrder o :list){
						System.out.println(o.getOrderNo());
						if(o.getOrderNo().equals("23439461025298318")){
							parseAliTaobaoProduct(o);
						}
					}

				} else {
					System.out.println("errrrrrrrrrrrrrrrrrrrrrrrrrr");
				}
			}
			// 用户短信
		} 
		
  }
  public static  void parseAliTaobaoProduct ( AliTaobaoOrder o) throws IOException, InterruptedException{
		List<AliTaobaoOrderProduct> list = o.getProducts();
		if(list==null || list.size()<=0){
			return;
		}
		String s="";
		StringBuilder sb = new StringBuilder(200);
		 
		sb.append(o.getOrderNo()==null?"":o.getOrderNo()).append(CRT001); 
		for(AliTaobaoOrderProduct p :list){
			//店名
			sb.delete(0, sb.length());
			sb.append(p.getShopName()==null?"":p.getShopName()).append(CRT001);
			//商品原价 单位分
			sb.append(p.getOriginal()==null?"":p.getOriginal()).append(CRT001);
			//商品成交价 单位分
			sb.append(p.getPromotion()==null?"":p.getPromotion()).append(CRT001);
			//商品名称
			sb.append(p.getName()==null?"":p.getName()).append(CRT001);
			//购买的商品数量
			sb.append(p.getQuantity()==null?"":p.getQuantity()).append(CRT001);
			//商品属性
			sb.append(p.getAttr()==null?"":p.getAttr()).append(CRT001);
			//商品权益
			sb.append(p.getServiceInfo()==null?"":p.getServiceInfo());
			s=sb.toString().replace("\n", " ");
			
			System.out.println(sb.toString());
  		 
		}
		
	}
}
